﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml.Linq;

namespace Als.Utils.Pay.UnionPay
{
    public class Utils
    {
        private static string _merId = "";
        private static string _version = "";
        private static string _encoding = "";
        private static string _signMethod = "";
        private static string _txnType = "";
        private static string _txnSubType = "";
        private static string _bizType = "";
        private static string _channelType = "";
        private static string _accessType = "";
        private static string _currencyCode = "";
        private static string _signCertpwd = "";
        private static string _frontTransUrl = "";
        private static string _certPath = "";
        static Utils()
        {
            initConfig();
        }

        private static void initConfig()
        {
            var path = HttpContext.Current.Server.MapPath("/Config/UnionPayConfig.xml");
            var doc = XDocument.Load(path);
            var config = doc.Element("Config");
            _merId = config.Element("MerId").Value;
            _version = config.Element("Version").Value;
            _encoding = config.Element("Encoding").Value;
            _signMethod = config.Element("SignMethod").Value;
            _txnType = config.Element("TxnType").Value;
            _txnSubType = config.Element("TxnSubType").Value;
            _bizType = config.Element("BizType").Value;
            _channelType = config.Element("ChannelType").Value;
            _accessType = config.Element("AccessType").Value;
            _currencyCode = config.Element("CurrencyCode").Value;
            _signCertpwd = config.Element("SignCertpwd").Value;
            _frontTransUrl = config.Element("FrontTransUrl").Value;
            _certPath = HttpContext.Current.Server.MapPath(config.Element("CertPath").Value);
        }

        public static string SendRequest(string orderId, decimal totalFee, string productInfo, string notifyUrl, string returnUrl)
        {
            var data = new Dictionary<string, string>
            {
                {"merId", _merId},
                {"version", _version},
                {"encoding", _encoding},
                {"signMethod", _signMethod},
                {"txnType", _txnType},
                {"txnSubType", _txnSubType},
                {"bizType", _bizType},
                {"channelType", _channelType},
                {"accessType", _accessType},
                {"currencyCode", _currencyCode},
                {"frontUrl", returnUrl},
                {"backUrl", notifyUrl},
                {"orderId", orderId},
                {"txnTime", DateTime.Now.ToString("yyyyMMddHHmmss")},
                {"txnAmt", ((int) (totalFee * 100M)).ToString()}
            };


            System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/err.log"), _certPath);
            try
            {
                if (!SignUtil.Sign(data, Encoding.GetEncoding(_encoding), _certPath, _signCertpwd))
                {
                    throw new Exception("签名失败！");
                }
            }
            catch (Exception exception)
            {
                System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/err.log"), "333333");
                throw new Exception(exception.Message);
            }
            return (_frontTransUrl + "?" + SignUtil.CoverDictionaryToString(data));
        }
    }
}
